package com.qzsoft.zhjd.common.aop;

import com.qzsoft.zhjd.common.annotation.ValidatedMap;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import org.springframework.core.MethodParameter;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
@CrossOrigin
public class ValidatedInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 对于注解的判断---------------------------------------------------
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        ValidatedMap methodAnnotation = handlerMethod.getMethodAnnotation(ValidatedMap.class);
        if(methodAnnotation!=null){
            // 如果自己拥有NoNeedToken标注或者所属的class拥有NoNeedToken 就直接放行
            MethodParameter[] methodParameters = ((HandlerMethod) handler).getMethodParameters();
            if(null != methodParameters && methodParameters.length>0){
//                Class<?> response1 = handlerMethod.getMethodAnnotation(ValidatedMap.class).response();
//                Field[] declaredFields = handlerMethod.getMethodAnnotation(ValidatedMap.class).response().getDeclaredFields();
                Map<String,Object> params = new HashMap<>();
                Map<String, String[]> parameterMap = request.getParameterMap();
                if(!CollectionUtils.isEmpty(parameterMap)){
                    for(String key:parameterMap.keySet()){
                        if (parameterMap.get(key).length == 1) {
                            params.put(key,parameterMap.get(key)[0]);
                        } else if (parameterMap.get(key).length > 1) {
                            params.put(key,parameterMap.get(key));
                        }
                    }
                }
            }
        }
        return true;
    }

    public void chargeNotNull(Object o,Field[] declaredFields)throws Exception{
        if(null != declaredFields && declaredFields.length >0){
            for(int s = 0;s<declaredFields.length;s++){
                Field declaredField = declaredFields[s];
                if(null != declaredField.getAnnotation(NotBlank.class)){
                    String message = declaredField.getAnnotation(NotBlank.class).message();
//                    String name = declaredField.getName();
                    declaredField.setAccessible(true);
                    if(declaredField.get(o)== null){
                        RenExceptionUtils.throwEx(message);
                    }
                }
            }
        }
    }
}
